package com.yocard.yxyd.ui.activity;

import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.CacheDoubleStaticUtils;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.others.CommonTimer;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.ChangePhoneContract;
import com.yocard.yxyd.ui.presenter.ChangePhonePresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2019/4/10.
 */
public class ChangePhoneActivity extends BaseActivity<ChangePhonePresenter> implements ChangePhoneContract.View {

    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.et_mobile)
    AppCompatEditText etMobile;
    @BindView(R.id.et_code)
    AppCompatEditText etCode;
    protected CommonTimer myTimer;

    @Override
    protected int getLayout() {
        return R.layout.act_change_phone;
    }

    @Override
    protected void initEventAndData() {
        initTitle();
        btnNext.setEnabled(false);
        etMobile.addTextChangedListener(textWatcher);
        etCode.addTextChangedListener(textWatcher);
        myTimer = new CommonTimer(60 * 1000, 1000, tvGetCode);
    }

    private void initTitle() {
        ImmersionBar.with(this).statusBarColor(R.color.white)
                .statusBarDarkFont(true).navigationBarColor(R.color.white).init();
        tvTitle.setText("安全验证");
    }

    @OnClick({R.id.iv_back, R.id.btn_next,R.id.tv_get_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ActivityUtils.finishActivity(this);
                break;
            case R.id.tv_get_code:
                //防止短时间重复点击
                if (isFastDoubleClick()) return;

                showLoadingDialog();
                String mobileNew = etMobile.getText().toString().trim();
                mPresenter.getNewMobileCode(mobileNew);
                break;
            case R.id.btn_next:
                showLoadingDialog();
                String mobile = etMobile.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                mPresenter.changeMobile(code,mobile);
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void OnChangeSuccess() {
        dismisDialog();
        ToastUtils.showLong("更换手机号成功");
        CacheDoubleStaticUtils.remove(Constants.KEY_TOKEN);
        ActivityUtils.startActivity(LoginActivity.class);
    }

    @Override
    public void onNewCodeGet() {
        tvGetCode.setEnabled(false);
        myTimer.start();
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String mobile = etMobile.getText().toString().trim();
            String code = etCode.getText().toString().trim();
            if(mobile.length() == 11){
                etCode.requestFocus();
            }

            if(mobile.length() == 11 && code.length() == 4){
                btnNext.setEnabled(true);
            }else {
                btnNext.setEnabled(false);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(myTimer != null){
            myTimer.cancel();
        }
    }
}
